from django.http import HttpResponse
import json
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlencode


proxies = {
        "http": "http://qf:qf3.1415@202.112.118.7:10086/"
    }

#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")


    lis = soup.select(".Ie-tableContainer tbody > tr")


    for li in lis:
        tempData = resultData()

        try:
            tempData["titleName"] = li.select(".Link")[0].text.strip()
        except:
            pass
        try:
            tempData["titleHref"] = "http://www.infobank.cn" + li.select(".Link")[0]["href"]
        except:
            pass
        try:
            tempData["publishTime"] = li.select("td")[5].text.strip()
        except:
            pass
        try:
            tempData["source"] = li.select("td")[4].text.strip()
        except:
            pass
        try:
            tempData["author"] = [li.select("td")[3].text.strip()]
        except:
            pass
        allData.append(tempData)

    return allData

def getData(keyword):
    headers = {
        'Connection': 'keep-alive',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Referer': 'https://dlib.eastview.com/search/simple/articles?pager.offset=30',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    params = (
        ('_isTranslite', 'on'),
        ('dateRangeType', 'predefined'),
        ('predefined', '0'),
        ('fromDay', '1'),
        ('fromMonth', '0'),
        ('fromYear', '1773'),
        ('toDay', '23'),
        ('toMonth', '1'),
        ('toYear', '2019'),
        ('searchForOriginal', keyword),
        ('udbIds', ''),
    )
    proxies = {
        "https": "https://qf:qf3.1415@202.112.118.7:10086/"
    }

    response = requests.get('https://dlib.eastview.com/search/simple/articles', headers=headers, params=params,proxies=proxies)

    allData = formData(response)

    return allData


def index(request):

    data = json.loads(request.POST.get('data', '1'))

    allData = getData(data["values"][0])

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")